package com.isoftstone.common.base;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

public class MCHPost {

	private  static final Logger log = LoggerFactory.getLogger(MCHPost.class);   
	public static String post(String addr, String send, HttpServletRequest request){
		StringBuffer html = new StringBuffer();
		try {
	        HttpClient client = new HttpClient();
			PostMethod post = new PostMethod("https://openapi.boc.cn/app/useridquery");
			//22.11.84.38
			client.setTimeout(300*1000);
			//post.setRequestHeader("Content-Type","text/xml");
			//post.setRequestHeader("charset","utf-8");


			post.setRequestHeader("clentid", "428");
			post.setRequestHeader("userid", "15102929211");
			post.setRequestHeader("acton", (String) request.getSession().getAttribute("accessToken"));
			post.setRequestHeader("chnflg", "1");
			post.setRequestHeader("trandt", "20170817");
			post.setRequestHeader("trantm", "224530");

			post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
			// NameValuePair[] data = {new NameValuePair("xml",send)};
			// post.setRequestBody(data);
			InputStream rbody = new   ByteArrayInputStream("".getBytes("UTF-8"));
			post.setRequestBody(rbody);
			int stauts = client.executeMethod(post);
			client.setTimeout(30*1000);
			if (stauts == HttpStatus.SC_OK){
				
				InputStream txtis = post.getResponseBodyAsStream();
				if (txtis != null) {
					BufferedReader br = new BufferedReader(
							new InputStreamReader(txtis,"UTF-8"));
					int count = 0;
					String tempbf;
					String ret = null;
					html.setLength(0);
					while ((tempbf = br.readLine()) != null) {
						if (count == 0)
							ret = tempbf;
						++count;
						html.append(tempbf);
						html.append("\r\n");
					}
					try {
						if (html.toString().trim().length() == 0) {
							throw  new Exception("发生错误");
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				} 
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		System.out.println("html ===================== " + html.toString());
		return html.toString();
	}
	public static String post2(String addr,String send){
		StringBuffer html = new StringBuffer();
		try {
	        HttpClient client = new HttpClient();
			PostMethod post = new PostMethod(addr);
			//22.11.84.38
			client.setTimeout(300*1000);
			post.setRequestHeader("Content-Type","text/xml");
			post.setRequestHeader("charset","utf-8");
			log.info("商户发送的报文");
			log.info("-------------");
			log.info(send);
			InputStream rbody = new   ByteArrayInputStream(send.getBytes("UTF-8"));
			post.setRequestBody(rbody);
			int stauts = client.executeMethod(post);
			client.setTimeout(30*1000);
			if (stauts == HttpStatus.SC_OK){
				
				InputStream txtis = post.getResponseBodyAsStream();
				if (txtis != null) {
					BufferedReader br = new BufferedReader(
							new InputStreamReader(txtis,"UTF-8"));
					int count = 0;
					String tempbf;
					String ret = null;
					html.setLength(0);
					while ((tempbf = br.readLine()) != null) {
						if (count == 0)
							ret = tempbf;
						++count;
						html.append(tempbf);
						html.append("\r\n");
					}
					try {
						if (html.toString().trim().length() == 0) {
							throw  new Exception("发生错误");
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
					log.info("模拟商户接收到的数据");
					log.info("-------------------");
					log.info(html.toString());
				} 
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return html.toString();
	}
	public static void main(String[] args){
		
	}
		
		
	
}
